import Cocoa
import FlutterMacOS

public class YFlutterPlugin: NSObject, FlutterPlugin {
    static var flutterViewController:FlutterViewController!

    public static func register(with flutterViewController:FlutterViewController) -> FlutterViewController {
        self.flutterViewController = flutterViewController
        return flutterViewController
    }

    public static func yRunFlutter(className: String, methodName: String, params: [String: Any]? = nil, callBack: ((Any) -> ())? = nil) {
        FlutterMethodChannel(name: className, binaryMessenger: flutterViewController.engine.binaryMessenger).invokeMethod(methodName, arguments: params) { (result) in
            if result != nil { callBack?(result!) }
        }
    }

    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(name: "YFreePlugin", binaryMessenger: registrar.messenger)
        let instance = YFlutterPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }

    public func handle(_ methodCall: FlutterMethodCall, result: @escaping FlutterResult) {
        let arguments = methodCall.arguments as? NSDictionary
        switch methodCall.method {
        case "yAesEncrypt":
            result(YAESCrypt.encrypt(arguments!["content"] as! String, password:arguments!["password"] as! String))
        case "yAesDecrypt":
            result(YAESCrypt.decrypt(arguments!["content"] as! String, password:arguments!["password"] as! String))
        default: result(FlutterMethodNotImplemented)
        }
    }
}
